home *** CD-ROM | disk | FTP | other *** search
/ Multimedia Jumpstart / Multimedia Microsoft Jumpstart Version 1.1a (Microsoft).BIN / develpmt / source / palfx / palfx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-21  |  23.9 KB  |  912 lines

  1. /*
  2.  *
  3.  *   PROGRAM: palfx.c
  4.  *
  5.  *    (C) Copyright Microsoft Corp. 1991, 1992.  All rights reserved.
  6.  *
  7.  *    You have a royalty-free right to use, modify, reproduce and 
  8.  *    distribute the Sample Files (and/or any modified version) in 
  9.  *    any way you find useful, provided that you agree that 
  10.  *    Microsoft has no warranty obligations or liability for any 
  11.  *    Sample Application Files which are modified. 
  12.  *
  13.  */
  14.  
  15. #include <windows.h>
  16. #include <commdlg.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "palfx.h"
  20. #include "dib.h"
  21. #include "gmem.h"
  22.  
  23. HANDLE hInst;
  24.  
  25. OPENFILENAME ofn;
  26. char        szFilters[] = "Bitmaps\0*.bmp;*.dib\0"
  27.                           "All Files\0*.*\0"
  28.                           "";
  29.  
  30. char    DIBFileName[_MAX_FNAME];
  31. char    TargetFileName [_MAX_FNAME];
  32. char    OpenName[_MAX_FNAME];
  33. char    str[255];
  34.  
  35. #include <sys\types.h>
  36. #include <sys\stat.h>
  37.  
  38.  
  39. PALETTEENTRY    gapeOld[256];        /* original color table */
  40. PALETTEENTRY    gapeNew[256];        /* modified color table */
  41. int        gcpe;            /* size of color table */
  42.  
  43.  
  44. HMENU hPalFxMenu;
  45. HANDLE hHourGlass;                        /* handle to hourglass cursor    */
  46. int    hFile;                                /* file handle                   */
  47. OFSTRUCT OfStruct;                        /* information from OpenFile()   */
  48. struct stat FileStatus;                   /* information from fstat()      */
  49. BOOL bFade = FALSE;                        /* TRUE if the fade in progress  */
  50. RECT Rect;                                /* dimension of the client window  */
  51. HWND hwnd;                                /* handle to main window           */
  52.  
  53. HANDLE hCurrDib = NULL;                         /* DIB currently displayed */
  54. HPALETTE hpalCurrent = NULL;                    /* Palette for hCurrDib */
  55.  
  56. char *szFxClass="PalFxWClass";    // window class name
  57.  
  58. /* Fade to Black data */
  59. #define FADEPERIOD 100    // set lower to fade faster
  60. #define FADETIMER_ID 1
  61. #define MAXCOLORS 256
  62. #define DARKENBY 10
  63.  
  64. #define LINESCROLL    1
  65. #define PAGESCROLL    20
  66.  
  67.  
  68. PALETTEENTRY FadePal[MAXCOLORS];
  69. HANDLE hBaseDIB;
  70. HPALETTE hpal;
  71. int    nNumFades, nNumColors;
  72.  
  73. /* Fade to target DIB data */
  74. #define TARGETTIMER_ID 2
  75. int    nNumTargetColors;
  76. HPALETTE hTargetPal;
  77. PALETTEENTRY TargetPal[MAXCOLORS];
  78. PALETTEENTRY TempPal[MAXCOLORS];
  79. BOOL bFadeToDIB = FALSE;
  80. HANDLE hTargetDIB;
  81.  
  82. PALETTEENTRY TestPal[MAXCOLORS];
  83. PALETTEENTRY SysPal[MAXCOLORS];
  84.  
  85. /* Needed for saving system colors */
  86. BOOL bStatic = TRUE;
  87. int    SysPalIndex [] = 
  88. {
  89.     COLOR_ACTIVEBORDER,
  90.     COLOR_ACTIVECAPTION,
  91.     COLOR_APPWORKSPACE,
  92.     COLOR_BACKGROUND,
  93.     COLOR_BTNFACE,
  94.     COLOR_BTNSHADOW,
  95.     COLOR_BTNTEXT,
  96.     COLOR_CAPTIONTEXT,
  97.     COLOR_GRAYTEXT,
  98.     COLOR_HIGHLIGHT,
  99.     COLOR_HIGHLIGHTTEXT,
  100.     COLOR_INACTIVEBORDER,
  101.     COLOR_INACTIVECAPTION,
  102.     COLOR_MENU,
  103.     COLOR_MENUTEXT,
  104.     COLOR_SCROLLBAR,
  105.     COLOR_WINDOW,
  106.     COLOR_WINDOWFRAME,
  107.     COLOR_WINDOWTEXT
  108. };
  109.  
  110.  
  111. /* Define the black-and-white color settings */
  112.  
  113. #define NumSysColors (sizeof (SysPalIndex) / sizeof (SysPalIndex[0]))
  114. DWORD OldColors [NumSysColors];
  115.  
  116. #define rgbBlack RGB(0,0,0)
  117. #define rgbWhite RGB(255,255,255)
  118.  
  119. DWORD MonoColors[NumSysColors] = 
  120. {
  121.     rgbWhite,              //    ACTIVEBORDER
  122.     rgbWhite,              //    ACTIVECAPTION
  123.     rgbWhite,              //    APPWORKSPACE
  124.     rgbWhite,              //    BACKGROUND
  125.     rgbWhite,              //    BTNFACE
  126.     rgbBlack,              //    BTNSHADOW
  127.     rgbBlack,              //    BTNTEXT
  128.     rgbBlack,              //    CAPTIONTEXT
  129.     rgbBlack,              //    GRAYTEXT
  130.     rgbBlack,              //    HIGHLIGHT
  131.     rgbWhite,              //    HIGHLIGHTTEXT
  132.     rgbWhite,              //    INACTIVEBORDER
  133.     rgbWhite,              //    INACTIVECAPTION
  134.     rgbWhite,              //    MENU
  135.     rgbBlack,              //    MENUTEXT
  136.     rgbWhite,              //    SCROLLBAR
  137.     rgbWhite,              //    WINDOW
  138.     rgbBlack,              //    WINDOWFRAME
  139.     rgbBlack                //    WINDOWTEXT
  140. };
  141.  
  142.  
  143. void AppPaint (HWND hwnd, HDC hdc);
  144. void FadeToBlack (HWND hWnd, HANDLE hDib);
  145. void FadeOneStep ();
  146. void BlackScreen ();
  147. void InitBaseDIB (HWND hWnd, LPSTR FileName);
  148. void InitTargetDIB (HWND hWnd, LPSTR FileName);
  149. void InitTargetDisplay (HWND hWnd);
  150. void BrightenOneStep ();
  151. void StealColors ();
  152. void AppActivate (BOOL fActive);
  153.  
  154.  
  155. /*
  156.  
  157.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  158.  
  159.     PURPOSE: calls initialization function, processes message loop
  160.  
  161.  */
  162.  
  163. int    PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  164. HANDLE hInstance;
  165. HANDLE hPrevInstance;
  166. LPSTR lpCmdLine;
  167. int    nCmdShow;
  168. {
  169.     MSG msg;
  170.  
  171.     if (!hPrevInstance)
  172.         if (!InitApplication(hInstance))
  173.             return (FALSE);
  174.  
  175.     if (!InitInstance(hInstance, nCmdShow))
  176.         return (FALSE);
  177.  
  178.     while (GetMessage(&msg, NULL, NULL, NULL)) 
  179.     {
  180.  
  181.         TranslateMessage(&msg);
  182.         DispatchMessage(&msg);
  183.     }
  184.     return (msg.wParam);
  185. }
  186.  
  187.  
  188. /*
  189.  
  190.     FUNCTION: InitApplication(HANDLE)
  191.  
  192.     PURPOSE: Initializes window data and registers window class
  193.  
  194.  */
  195.  
  196. BOOL InitApplication(hInstance)
  197. HANDLE hInstance;
  198. {
  199.     WNDCLASS  wc;
  200.  
  201.     wc.style = NULL;
  202.     wc.lpfnWndProc = MainWndProc;
  203.     wc.cbClsExtra = 0;
  204.     wc.cbWndExtra = 0;
  205.     wc.hInstance = hInstance;
  206.     wc.hIcon = LoadIcon(hInstance, "PalFxIcon");
  207.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  208.     wc.hbrBackground = GetStockObject(BLACK_BRUSH);
  209.     wc.lpszMenuName =  MAKEINTRESOURCE(PALFXMENU);
  210.     wc.lpszClassName = szFxClass;
  211.  
  212.     return (RegisterClass(&wc));
  213. }
  214.  
  215.  
  216. /****************************************************************************
  217.  
  218.     FUNCTION:  InitInstance(HANDLE, int)
  219.  
  220.     PURPOSE:  Saves instance handle and creates main window
  221.  
  222. ****************************************************************************/
  223.  
  224. BOOL InitInstance(hInstance, nCmdShow)
  225. HANDLE          hInstance;
  226. int    nCmdShow;
  227. {
  228.     RECT            Rect;
  229.  
  230.     hInst = hInstance;
  231.  
  232.     hwnd = CreateWindow(
  233.         szFxClass,
  234.         "Palette FX Demo",
  235.         WS_OVERLAPPEDWINDOW,
  236.         CW_USEDEFAULT, 0,
  237.         CW_USEDEFAULT, 0,
  238.         NULL,
  239.         NULL,
  240.         hInstance,
  241.         NULL
  242.         );
  243.  
  244.     if (!hwnd)
  245.         return (FALSE);
  246.  
  247.     GetClientRect(hwnd, (LPRECT) & Rect);
  248.  
  249.     /* Get an hourglass cursor to use during file transfers */
  250.  
  251.     hHourGlass = LoadCursor(NULL, IDC_WAIT);
  252.     hPalFxMenu = GetMenu (hwnd);
  253.  
  254.     ShowWindow(hwnd, SW_SHOWMAXIMIZED);
  255.     return (TRUE);
  256. }
  257.  
  258.  
  259. long    FAR PASCAL MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  260. {
  261.     FARPROC lpProcAbout;
  262.  
  263.  
  264.     HDC hdc;                                          /* context handle */
  265.     PAINTSTRUCT ps;
  266.     int    i;
  267.     HPALETTE hpalPrev;
  268.  
  269.     switch (message)
  270.     {
  271.         case WM_PALETTECHANGED:
  272.  
  273.         /* if my window caused the palette change, do nothing */
  274.         if (wParam == hwnd)
  275.         break;
  276.  
  277.         /* fall through */
  278.  
  279.      case WM_QUERYNEWPALETTE:
  280.  
  281.          /* process WM_PALETTECHANGED or WM_QUERYNEWPALETTE:
  282.           * temporarily select and realize current palette to see
  283.           * if a full redraw is required (due to some of the physical
  284.               * palette entries <hwnd> uses being changed)
  285.               */
  286.          if (hpalCurrent == NULL)
  287.          return (long) FALSE;    // no palette to realize
  288.              
  289.          hdc = GetDC(hwnd);
  290.          hpalPrev = SelectPalette(hdc, hpalCurrent, FALSE);
  291.          i = RealizePalette(hdc);
  292.          SelectPalette(hdc, hpalPrev, FALSE);
  293.          ReleaseDC(hwnd, hdc);
  294.  
  295.          if (i)
  296.          InvalidateRect(hwnd, NULL, TRUE);
  297.  
  298.          return (LONG) i;
  299.  
  300.  
  301.  
  302.      case WM_CREATE:
  303.          break;
  304.  
  305.      case WM_ACTIVATEAPP:
  306.          AppActivate (TRUE);        /* Steal system colors. */
  307.          bStatic = FALSE;
  308.          break;
  309.  
  310.      case WM_COMMAND:
  311.          switch (wParam)
  312.          {
  313.          case IDM_ABOUT:
  314.              lpProcAbout = MakeProcInstance(About, hInst);
  315.              DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
  316.              FreeProcInstance(lpProcAbout);
  317.              break;
  318.  
  319.          case IDM_REVERT:
  320.              EnableMenuItem (hPalFxMenu, IDM_TOBLACK, MF_ENABLED);
  321.              EnableMenuItem (hPalFxMenu, IDM_TODIB, MF_ENABLED);
  322.              BlackScreen ();
  323.              InitBaseDIB (hwnd, DIBFileName);
  324.              EnableMenuItem (hPalFxMenu, IDM_REVERT, MF_GRAYED);
  325.              break;
  326.  
  327.          case IDM_TOBLACK:
  328.              EnableMenuItem (hPalFxMenu, IDM_TOBLACK, MF_GRAYED);
  329.              EnableMenuItem (hPalFxMenu, IDM_TODIB, MF_GRAYED);
  330.              FadeToBlack (hwnd, hBaseDIB);
  331.              break;
  332.  
  333.          case IDM_TODIB:
  334.              EnableMenuItem (hPalFxMenu, IDM_TOBLACK, MF_GRAYED);
  335.              EnableMenuItem (hPalFxMenu, IDM_TODIB, MF_GRAYED);
  336.  
  337.              
  338.              OpenName[0] = 0;
  339.              ofn.lStructSize         = sizeof(ofn);
  340.              ofn.hwndOwner           = hwnd;
  341.              ofn.hInstance           = hInst;
  342.              ofn.lpstrFilter         = szFilters;
  343.              ofn.nFilterIndex        = 1;
  344.              ofn.lpstrCustomFilter   = NULL;
  345.              ofn.nMaxCustFilter      = 0;
  346.              ofn.lpstrFile           = OpenName;
  347.              ofn.nMaxFile            = sizeof(OpenName);
  348.              ofn.lpstrFileTitle      = "";
  349.              ofn.nMaxFileTitle       = 0;
  350.              ofn.lpstrInitialDir     = NULL;
  351.              ofn.lpstrTitle          = "Open File";
  352.              ofn.Flags               = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  353.              ofn.lCustData           = 0;
  354.              ofn.nFileOffset         = 0;
  355.              ofn.nFileExtension      = 0;
  356.              ofn.lpstrDefExt         = NULL;
  357.              ofn.lpfnHook            = NULL;
  358.              ofn.lpTemplateName      = NULL;
  359.  
  360.              if (!GetOpenFileName(&ofn))
  361.              return 0L;
  362.  
  363.              if ((hFile = OpenFile(OpenName, (LPOFSTRUCT) & OfStruct,
  364.              OF_READ)) == -1) 
  365.              {
  366.              wsprintf(str, "Error %d opening %s.",
  367.                  OfStruct.nErrCode, (LPSTR)OpenName);
  368.              MessageBox(hwnd, str, NULL,
  369.                  MB_OK | MB_ICONHAND);
  370.              return (NULL);
  371.              }
  372. #if 0             
  373.              lpOpenDlg = MakeProcInstance((FARPROC) OpenDlg, hInst);
  374.              /* Open the file and get its handle */
  375.              hFile = DialogBox(hInst, "Open", hWnd, lpOpenDlg);
  376.              FreeProcInstance(lpOpenDlg);
  377.              if (!hFile)
  378.              return (NULL);
  379. #endif             
  380.              lstrcpy( (LPSTR)TargetFileName, (LPSTR)OpenName);
  381.              _lclose (hFile);
  382.              InitTargetDIB (hwnd, TargetFileName);
  383.              bFadeToDIB = TRUE;
  384.              FadeToBlack (hwnd, hBaseDIB);
  385.              break;
  386.  
  387.  
  388.          case IDM_OPEN:
  389.              OpenName[0] = 0;
  390.              ofn.lStructSize         = sizeof(ofn);
  391.              ofn.hwndOwner           = hwnd;
  392.              ofn.hInstance           = hInst;
  393.              ofn.lpstrFilter         = szFilters;
  394.              ofn.nFilterIndex        = 1;
  395.              ofn.lpstrCustomFilter   = NULL;
  396.              ofn.nMaxCustFilter      = 0;
  397.              ofn.lpstrFile           = OpenName;
  398.              ofn.nMaxFile            = sizeof(OpenName);
  399.              ofn.lpstrFileTitle      = "";
  400.              ofn.nMaxFileTitle       = 0;
  401.              ofn.lpstrInitialDir     = NULL;
  402.              ofn.lpstrTitle          = "Open File";
  403.              ofn.Flags               = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  404.              ofn.lCustData           = 0;
  405.              ofn.nFileOffset         = 0;
  406.              ofn.nFileExtension      = 0;
  407.              ofn.lpstrDefExt         = NULL;
  408.              ofn.lpfnHook            = NULL;
  409.              ofn.lpTemplateName      = NULL;
  410.  
  411.              if (!GetOpenFileName(&ofn))
  412.              return 0L;
  413.  
  414.              if ((hFile = OpenFile(OpenName, (LPOFSTRUCT) & OfStruct,
  415.              OF_READ)) == -1) 
  416.              {
  417.              wsprintf(str, "Error %d opening %s.",
  418.                  OfStruct.nErrCode, (LPSTR)OpenName);
  419.              MessageBox(hwnd, str, NULL,
  420.                  MB_OK | MB_ICONHAND);
  421.              return (NULL);
  422.              }
  423. #if 0             
  424.              lpOpenDlg = MakeProcInstance((FARPROC) OpenDlg, hInst);
  425.  
  426.              /* Open the file and get its handle */
  427.  
  428.              hFile = DialogBox(hInst, "Open", hWnd, lpOpenDlg);
  429.              FreeProcInstance(lpOpenDlg);
  430.              if (!hFile)
  431.              return (NULL);
  432. #endif             
  433.              lstrcpy( DIBFileName, OpenName);
  434.              _lclose (hFile);
  435.              InitBaseDIB (hwnd, DIBFileName);
  436.              EnableMenuItem (hPalFxMenu, IDM_TOBLACK, MF_ENABLED);
  437.              EnableMenuItem (hPalFxMenu, IDM_TODIB, MF_ENABLED);
  438.              break;
  439.  
  440.          case IDM_EXIT:
  441.              DestroyWindow(hWnd);
  442.              break;
  443.              
  444.         default:
  445.             break;
  446.  
  447.         }
  448.         return NULL;
  449.  
  450.     case WM_LBUTTONDOWN:                    /* Mouse input */
  451.         if (bFade)
  452.         {
  453.         }
  454.         break;
  455.  
  456.     case WM_TIMER:
  457.         switch (wParam )
  458.         {
  459.         case FADETIMER_ID:
  460.         /* Perform fade if necessary. */
  461.         if (nNumFades > 0)
  462.         {
  463.             FadeOneStep ();
  464.             nNumFades--;
  465.         }
  466.         else
  467.         {
  468.             KillTimer (hwnd, FADETIMER_ID);
  469.             if (bFadeToDIB)
  470.             {
  471.             /* Display blackened target DIB */
  472.             BlackScreen ();
  473.             InitTargetDisplay (hwnd);
  474.             nNumFades = 256 / DARKENBY + 1;
  475.             SetTimer (hwnd, TARGETTIMER_ID, FADEPERIOD, NULL);
  476.             }
  477.             else
  478.             {
  479.             EnableMenuItem (hPalFxMenu, IDM_REVERT, MF_ENABLED);
  480.             }
  481.         }
  482.         break;
  483.         
  484.         case TARGETTIMER_ID:
  485.             if (nNumFades > 0)
  486.             {
  487.             BrightenOneStep ();
  488.             nNumFades--;
  489.             }
  490.             else
  491.             {
  492.             KillTimer (hwnd, TARGETTIMER_ID);
  493.             bFadeToDIB = FALSE;
  494.             EnableMenuItem (hPalFxMenu, IDM_REVERT, MF_ENABLED);
  495.             }
  496.             break;
  497.         }
  498.         return 0;
  499.  
  500.     case WM_PAINT:
  501.         hdc = BeginPaint(hWnd, &ps);
  502.         AppPaint (hWnd, hdc);
  503.         EndPaint(hWnd, &ps);
  504.         return 0;
  505.  
  506.     case WM_DESTROY:
  507.         AppActivate (FALSE);        /* Restore system colors. */
  508.         bStatic = TRUE;
  509.         if(hpalCurrent)
  510.         DeleteObject (hpalCurrent);
  511.         if (hBaseDIB)
  512.         GFree (hBaseDIB);
  513.         if(hTargetPal)
  514.         DeleteObject (hTargetPal);
  515.         if (hTargetDIB)
  516.         GFree (hTargetDIB);
  517.         PostQuitMessage(0);
  518.         break;
  519.  
  520.     default:
  521.         break;
  522.     }
  523.     return (DefWindowProc(hWnd, message, wParam, lParam));
  524. }
  525.  
  526.  
  527. /****************************************************************************
  528.  
  529.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  530.  
  531.     PURPOSE:  Processes messages for "About" dialog box
  532.  
  533.     MESSAGES:
  534.  
  535.         WM_INITDIALOG - initialize dialog box
  536.         WM_COMMAND    - Input received
  537.  
  538. ****************************************************************************/
  539.  
  540. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  541. HWND hDlg;
  542. unsigned    message;
  543. WORD wParam;
  544. LONG lParam;
  545. {
  546.     switch (message)
  547.     {
  548.     case WM_INITDIALOG:
  549.         return (TRUE);
  550.  
  551.     case WM_COMMAND:
  552.         if (wParam == IDOK
  553.              || wParam == IDCANCEL)
  554.         {
  555.             EndDialog(hDlg, TRUE);
  556.             return (TRUE);
  557.         }
  558.         break;
  559.     }
  560.     return (FALSE);
  561. }
  562.  
  563.  
  564. /****************************************************************************
  565.  
  566.     FUNCTION: AppPaint
  567.  
  568.     PURPOSE:  Repaint the current display surface.
  569.  
  570.     This function depends on GLOBALS!!! (ack)
  571.     
  572. ****************************************************************************/
  573.  
  574. void AppPaint (HWND hwnd, HDC hdc)
  575. {
  576.     BITMAPINFOHEADER bi;
  577.     WORD   xPos, yPos;
  578.     RECT rc;
  579.  
  580.     if (hCurrDib)
  581.     {
  582.     DibInfo (hCurrDib, &bi);    /* Redisplay the current DIB */
  583.         GetClientRect(hwnd, &rc);
  584.         xPos = (WORD) ((rc.right  - bi.biWidth ) / 2);
  585.         yPos = (WORD) ((rc.bottom - bi.biHeight) / 2);
  586.         DrawDib(hdc, xPos, yPos, hCurrDib, hpalCurrent, DIB_PAL_COLORS);
  587.     }
  588. }
  589.  
  590.  
  591. //    This function depends on GLOBALS!!! (ack)
  592. void InitBaseDIB (HWND hWnd, LPSTR FileName)
  593. {
  594.     LPBITMAPINFOHEADER lpbi = NULL;
  595.     RGBQUAD FAR * prgb;
  596.     int    i;
  597.     LOGPALETTE * pPal;
  598.     HANDLE hDib;
  599.  
  600.     hDib = OpenDIB (FileName);
  601.     /* Lock the DIB and get color info */
  602.     lpbi = (LPBITMAPINFOHEADER) GLock (hDib);
  603.     if (lpbi == NULL)
  604.         return;
  605.  
  606.     prgb = (RGBQUAD FAR * )((LPBYTE)lpbi + lpbi->biSize);
  607.  
  608.     nNumColors = DibNumColors(lpbi);
  609.  
  610.     if (nNumColors)
  611.     {
  612.     /* Allocate a new color table, copy the palette entries to
  613.          * it, and create the palette. 
  614.          */
  615.         pPal = (LOGPALETTE * )LocalAlloc(LPTR, 
  616.             sizeof(LOGPALETTE) + nNumColors * sizeof(PALETTEENTRY));
  617.  
  618.         if (!pPal)
  619.             return;
  620.  
  621.         pPal->palNumEntries = nNumColors;
  622.         pPal->palVersion    = PALVERSION;
  623.  
  624.         for (i = 0; i < nNumColors; i++)
  625.         {
  626.             pPal->palPalEntry[i].peRed   = prgb->rgbRed;
  627.             pPal->palPalEntry[i].peGreen = prgb->rgbGreen;
  628.             pPal->palPalEntry[i].peBlue  = prgb->rgbBlue;
  629.         pPal->palPalEntry[i].peFlags = PC_RESERVED;
  630.  
  631.         /* Create the palette to replace the original palette. */
  632.             FadePal[i].peRed   = prgb->rgbRed;
  633.             FadePal[i].peGreen = prgb->rgbGreen;
  634.             FadePal[i].peBlue  = prgb->rgbBlue;
  635.             FadePal[i].peFlags = PC_RESERVED;
  636.  
  637.             prgb++;
  638.         }
  639.  
  640.         GUnlock(hDib);
  641.     
  642.         hpal = CreatePalette(pPal);
  643.         LocalFree((HANDLE)pPal);
  644.  
  645.         SetDibUsage (hDib, hpal, DIB_PAL_COLORS);
  646.  
  647.         InvalidateRect(hWnd, NULL, TRUE);  // have apppaint do the work...
  648.  
  649.         hCurrDib = hDib;
  650.         hBaseDIB = hDib;
  651.     if(hpalCurrent)
  652.         DeleteObject (hpalCurrent);
  653.     hpalCurrent = hpal;
  654.     }
  655. }
  656.  
  657.  
  658. //    This function depends on GLOBALS!!! (ack)
  659. void InitTargetDIB (HWND hWnd, LPSTR FileName)
  660. {
  661.     LPBITMAPINFOHEADER lpbi = NULL;
  662.     RGBQUAD FAR * prgb;
  663.     int    i;
  664.     LOGPALETTE * pPal;
  665.     HANDLE hDib;
  666.  
  667.     hDib = OpenDIB (FileName);
  668.  
  669.     /* Lock the DIB and get color info */
  670.     lpbi = (LPBITMAPINFOHEADER) GLock (hDib);
  671.     if (lpbi == NULL)
  672.         return;
  673.  
  674.     // the next line was the problem...
  675.     prgb = (RGBQUAD FAR * )((LPBYTE)lpbi + lpbi->biSize);
  676.  
  677.     nNumTargetColors = DibNumColors(lpbi);
  678.  
  679.     if (nNumTargetColors)
  680.     {
  681.     /* Allocate a new color table, copy the palette entries to
  682.          * it, and create the palette. 
  683.          */
  684.         pPal = (LOGPALETTE * )LocalAlloc(LPTR, 
  685.             sizeof(LOGPALETTE) + nNumTargetColors * sizeof(PALETTEENTRY));
  686.  
  687.         if (!pPal)
  688.             return;
  689.  
  690.         pPal->palNumEntries = nNumTargetColors;
  691.         pPal->palVersion    = PALVERSION;
  692.  
  693.         for (i = 0; i < nNumTargetColors; i++)
  694.         {
  695.             pPal->palPalEntry[i].peRed     = prgb->rgbRed;
  696.             pPal->palPalEntry[i].peGreen = prgb->rgbGreen;
  697.             pPal->palPalEntry[i].peBlue  = prgb->rgbBlue;
  698.             pPal->palPalEntry[i].peFlags = PC_RESERVED;
  699.  
  700.         /* Create the palette to replace the current palette. */
  701.             TempPal[i].peRed = 0;
  702.             TempPal[i].peGreen = 0;
  703.             TempPal[i].peBlue = 0;
  704.             TempPal[i].peFlags = PC_RESERVED;
  705.             TargetPal[i].peRed     = prgb->rgbRed;
  706.             TargetPal[i].peGreen = prgb->rgbGreen;
  707.             TargetPal[i].peBlue  = prgb->rgbBlue;
  708.             TargetPal[i].peFlags = PC_RESERVED;
  709.  
  710.             prgb++;
  711.         }
  712.  
  713.         GUnlock(hDib);
  714.  
  715.     if(hTargetPal)
  716.         DeleteObject(hTargetPal);
  717.         hTargetPal = CreatePalette(pPal);
  718.         LocalFree((HANDLE)pPal);
  719.     
  720.         SetDibUsage (hDib, hTargetPal, DIB_PAL_COLORS);
  721.         SetPaletteEntries(hTargetPal, 0, nNumTargetColors, TempPal);
  722.  
  723.     if (hTargetDIB)
  724.         GFree (hTargetDIB);
  725.  
  726.         hTargetDIB = hDib;
  727.     }
  728. }
  729.  
  730.  
  731. void InitTargetDisplay (HWND hWnd)
  732. {
  733.     HDC hDC;
  734.     BITMAPINFOHEADER bi;
  735.     WORD  xPos, yPos;
  736.     HPALETTE hOldPal;
  737.     RECT rc;
  738.  
  739.     if (hTargetDIB)
  740.     {
  741.     /* Display the DIB */
  742.         hDC = GetDC(hWnd);
  743.         hOldPal = SelectPalette (hDC, hTargetPal, FALSE);
  744.     
  745.         UnrealizeObject(hTargetPal);
  746.  
  747.         RealizePalette (hDC);
  748.     
  749.     DibInfo (hTargetDIB, &bi);
  750.         GetClientRect(hWnd, &rc);
  751.         xPos =(WORD) ( (rc.right  - bi.biWidth ) / 2);
  752.         yPos =(WORD) ( (rc.bottom - bi.biHeight) / 2);
  753.  
  754.         DrawDib(hDC, xPos, yPos, hTargetDIB, hTargetPal, DIB_PAL_COLORS);
  755.         SelectPalette(hDC, hOldPal, FALSE);
  756.         ReleaseDC(hWnd, hDC);
  757.  
  758.     if (hCurrDib)
  759.         GFree (hCurrDib);
  760.     
  761.         hCurrDib = hTargetDIB;
  762.     
  763.     if(hpalCurrent)
  764.         DeleteObject (hpalCurrent);
  765.         
  766.         hpalCurrent = hTargetPal;
  767.     }
  768. }
  769.  
  770.  
  771.  
  772.  
  773. /****************************************************************************
  774.  
  775.     FUNCTION: FadeToBlack
  776.  
  777.     PURPOSE:  Gradually fades the given DIB to black.
  778.  
  779.     This function depends on GLOBALS!!! (ack)
  780.                      
  781. ****************************************************************************/
  782. void FadeToBlack (HWND hWnd, HANDLE hDib)
  783. {
  784.     /* Start timer for fade. */
  785.     nNumFades = 256 / DARKENBY +1;
  786.     SetTimer (hwnd, FADETIMER_ID, FADEPERIOD, NULL);
  787. }
  788.  
  789.  
  790. /****************************************************************************
  791.  
  792.     FUNCTION: FadeOneStep                      
  793.  
  794.     PURPOSE:
  795.  
  796.     This function depends on GLOBALS!!! (ack)
  797.                      
  798. ****************************************************************************/
  799. void FadeOneStep ()
  800. {
  801.     int    i, r, g, b;
  802.     HDC hdc;
  803.     HPALETTE hOldPal;
  804.  
  805.     /* Do one step of palette fade. */
  806.     for (i = 0; i < nNumColors; i++)
  807.     {
  808. #ifdef LINEARFADE        
  809.     // linear fade where 'bright' colors take longer to dissapear
  810.         r = FadePal[i].peRed   - DARKENBY;
  811.         g = FadePal[i].peGreen - DARKENBY;
  812.         b = FadePal[i].peBlue  - DARKENBY;
  813. #else    
  814.     // with this fade, all the colors dissapear at once no matter
  815.     // how 'bright' they were.
  816.         r = FadePal[i].peRed   - min(FadePal[i].peRed/nNumFades,DARKENBY);
  817.         g = FadePal[i].peGreen - min(FadePal[i].peGreen/nNumFades,DARKENBY);
  818.         b = FadePal[i].peBlue  - min(FadePal[i].peBlue/nNumFades,DARKENBY);
  819. #endif    
  820.  
  821.         FadePal[i].peRed   = (BYTE)max(r, 0);
  822.         FadePal[i].peGreen = (BYTE)max(g, 0);
  823.         FadePal[i].peBlue  = (BYTE)max(b, 0);
  824.         FadePal[i].peFlags = PC_RESERVED;
  825.     }
  826.  
  827.     hdc = GetDC(hwnd);
  828.     hOldPal=SelectPalette(hdc, hpalCurrent, FALSE);
  829.     RealizePalette(hdc);
  830.     AnimatePalette (hpalCurrent, 0, nNumColors, FadePal);
  831.     SelectPalette(hdc,hOldPal,FALSE);
  832.     ReleaseDC(hwnd, hdc);
  833. }
  834.  
  835.  
  836. /****************************************************************************
  837.  
  838.     FUNCTION: BrightenOneStep                      
  839.  
  840.     PURPOSE:
  841.     
  842.     This function depends on GLOBALS!!! (ack)
  843.                      
  844. ****************************************************************************/
  845. void BrightenOneStep ()
  846. {
  847.     int    i, r, g, b;
  848.     HDC hdc;
  849.     HPALETTE hOldPal;
  850.  
  851.     /* Do one step of palette brighten. */
  852.     for (i = 0; i < nNumTargetColors; i++)
  853.     {
  854.         r = max(0, TargetPal[i].peRed   - DARKENBY * nNumFades);
  855.         g = max(0, TargetPal[i].peGreen - DARKENBY * nNumFades);
  856.         b = max(0, TargetPal[i].peBlue  - DARKENBY * nNumFades);
  857.  
  858.         TempPal[i].peRed   = (BYTE)r;
  859.         TempPal[i].peGreen = (BYTE)g;
  860.         TempPal[i].peBlue  = (BYTE)b;
  861.         TempPal[i].peFlags = PC_RESERVED;
  862.     }
  863.  
  864.     hdc = GetDC(hwnd);
  865.     hOldPal=SelectPalette(hdc, hTargetPal, FALSE);
  866.     RealizePalette(hdc);
  867.     AnimatePalette (hTargetPal, 0, nNumTargetColors, TempPal);
  868.     SelectPalette(hdc,hOldPal,FALSE);
  869.     ReleaseDC(hwnd, hdc);
  870. }
  871.  
  872.  
  873.  
  874. /****************************************************************************
  875.  
  876.     FUNCTION: BlackScreen
  877.  
  878.     PURPOSE:                                         
  879.                      
  880. ****************************************************************************/
  881. void BlackScreen ()
  882. {
  883.     HDC hDC;
  884.     RECT rc;
  885.  
  886.     /* Blackout screen to avoid showing palette change */
  887.     hDC = GetDC(hwnd);
  888.     SetBkColor(hDC, 0);
  889.     GetClientRect(hwnd, &rc);
  890.     ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rc, "", 0, NULL);
  891.     ReleaseDC(hwnd, hDC);
  892.  
  893. }
  894.  
  895.  
  896. /****************************************************************************
  897.  
  898.     FUNCTION: AppActivate
  899.  
  900.     PURPOSE:  Saves the current system color values  and obtains the 
  901.                     use of the system colors when the application
  902.                     becomes active and restores the original system colors
  903.                     when the application indicates it is no longer active.
  904.                      
  905. ****************************************************************************/
  906. void AppActivate (BOOL fActive)
  907. {
  908.     return;
  909. }
  910.  
  911.  
  912.